package com.banmao.mall.service.client.impl;

import com.banmao.mall.mapper.AddressMapper;
import com.banmao.mall.mapper.RegionMapper;
import com.banmao.mall.model.bo.client.address.AddressDetailBO;
import com.banmao.mall.model.vo.client.address.AddressBaseVO;
import com.banmao.mall.model.vo.client.address.AddressDetailVO;
import com.banmao.mall.model.vo.client.address.RegionVO;
import com.banmao.mall.service.client.AddressSercice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressServiceImpl implements AddressSercice {

    @Autowired
    AddressMapper addressMapper;
    @Autowired
    RegionMapper regionMapper;


    @Override
    public List<AddressBaseVO> addressList(Integer userId) {

        List<AddressBaseVO> addressBaseVOS = addressMapper.selectAddrBaseByUserID(userId);

        for (AddressBaseVO addressBaseVO : addressBaseVOS) {
            // 遍历集合获取省、市、区地址
            String provinceName = addressMapper.selectWithRegion(addressBaseVO.getId(), "province_id");
            String cityName = addressMapper.selectWithRegion(addressBaseVO.getId(), "city_id");
            String areaName = addressMapper.selectWithRegion(addressBaseVO.getId(), "area_id");

            String detailedAddress = addressBaseVO.getDetailedAddress();
            addressBaseVO.setDetailedAddress(provinceName +
                                                cityName +
                                                areaName + "     " +
                                                detailedAddress);
        }

        return addressBaseVOS;
    }

    @Override
    public AddressDetailVO addressDetail(Integer addressId) {

        AddressDetailVO addressDetailVO = addressMapper.selectDetailAddr(addressId);

        // 根据省市区的ID获取值
        String provinceName = regionMapper.selectNameById(addressDetailVO.getProvinceId());
        String cityName = regionMapper.selectNameById(addressDetailVO.getCityId());
        String areaName = regionMapper.selectNameById(addressDetailVO.getAreaId());

        addressDetailVO.setProvinceName(provinceName);
        addressDetailVO.setCityName(cityName);
        addressDetailVO.setAreaName(areaName);

        return addressDetailVO;
    }

    @Override
    public int addressSave(AddressDetailBO addressDetailBO) {

        int result = 0;
        // 判断默认地址字段是否为true
        // 如果是true，将该user的所有地址的isDefaule字段设置为false
        if(addressDetailBO.getIsDefault()) {
            result = addressMapper.updateAllDefaultByUserID(addressDetailBO.getUserId());
            if(result == 0) {
                throw new RuntimeException("SQL出错了");
            }
        }

        // 判断地址ID
        // 0则添加新地址，否则更新
        if(addressDetailBO.getId() == 0) {
            // 添加操作
            result = addressMapper.insertAddrDetail(addressDetailBO);
            if(result == 0) {
                throw new RuntimeException("SQL出错了");
            }
        } else {
            // 更新操作
            result = addressMapper.updateAddrByCondition(addressDetailBO);
            if(result == 0) {
                throw new RuntimeException("SQL出错了");
            }
        }

        return addressDetailBO.getId();
    }

    @Override
    public List<RegionVO> regionList(Integer pid) {

        // 查找相同pid的地区列表
        List<RegionVO> regionVOS = addressMapper.selectRegionListByPID(pid);

        return regionVOS;
    }

    @Override
    public int addressDelete(Integer addressId) {

        int result = addressMapper.updateWithDeleted(addressId);

        return result;
    }
}
